<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile extends MY_Controller {

	function __construct() {
		parent::__construct();
	}
	
	public function index($profile_id)
	{
		if ($this->isSessionActive()) {

			$this->load->model('user_model');
			$this->load->model('offer_model');
			
			
			$data['user'] = $this->fb_id;
			$data['title'] = 'Profile';
			$data['content_view'] = 'profile_view';
			$data['stylesheets'] = array('profile');
			$data['javascripts'] = array('slider');
			
			$data['profile'] = $this->user_model->get_user($profile_id);
			$data['profile']['area_preference'] = $this->user_model->get_area_preference($profile_id);
			$data['profile']['category_preference'] = $this->user_model->get_category_preference($profile_id);			
			$data['category'] = $this->offer_model->get_category();
			//$data['sub_category'] = $this->offer_model->get_sub_category();
			$data['area_group'] = $this->user_model->get_area_group();
			$data['area'] = $this->user_model->get_area();
			
			$this->load->model('history_model');
			$data['successful_offers'] = count($this->history_model->get_successful_offers($this->fb_id));
			$data['successful_requests'] = count($this->history_model->get_successful_requests($this->fb_id));
			
			$this->load->model("home_model");
			$data['offers'] = $this->home_model->get_offers($profile_id);
			
			// check the fb_id and user_id to allow editing
			if ($data['user'] == $profile_id) {
				$data['edit'] = true;
				array_push($data['javascripts'],'category','area');
				
				if ($this->input->post('save_button_x')) {
					
					//get category preference
					$temp = $this->input->post("category_list");
					$category_list = json_decode($temp,true);
					$this->user_model->update_category_preference($data['user'],$category_list);
					
					//get area preference
					$temp = $this->input->post("area_list");
					$area_list = json_decode($temp,true);
					$this->user_model->update_area_preference($data['user'],$area_list);
					
					
					
					if ($this->input->post("newsletter_day") != 0 && $data['profile']['newsletter_day'] == 0) {
						$this->user_model->update_last_newsletter($data['user'], date('Y-m-d H:i:s'));
					}
					
					$data['profile']['other'] = $this->input->post("other_information");
					$data['profile']['newsletter_day'] = $this->input->post("newsletter_day");
					$newsletter_option = $this->input->post("newsletter_option");
					$data['profile']['new_offer'] = false;
					$data['profile']['new_request'] = false;
					$data['profile']['new_comment'] = false;
					$data['profile']['new_notification'] = false;
					for ($i = 0; $i < count($newsletter_option); ++$i) {
						switch ($newsletter_option[$i]) {
							case 1:
								$data['profile']['new_offer'] = true;
								break;
							case 2:
								$data['profile']['new_request'] = true;
								break;
							case 3:
								$data['profile']['new_comment'] = true;
								break;
							case 4:
								$data['profile']['new_notification'] = true;
								break;
						}
					}
					$this->user_model->update_profile($data['user'], $data['profile']['other'], $data['profile']['newsletter_day'], $data['profile']['new_offer'],
														$data['profile']['new_request'], $data['profile']['new_comment'], $data['profile']['new_notification']);
					
					$this->add_status_message('Your Profile has been edited!');
					redirect('/profile/' . $profile_id,'referesh');
				}		
				
			}
			else
				$data['edit'] = false;
			
			
			
			
    		$this->load_template_view($data, TRUE);
		}
	}
	
}